package com.example.kliven.because.fragment;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.kliven.because.R;
import com.example.kliven.because.activity.NearbyActivity;
import com.example.kliven.because.activity.RankingActivity;
import com.example.kliven.because.activity.RecommendActivity;
import com.example.kliven.because.adapter.DiscoverAdapter;
import com.example.kliven.because.bean.DiscoverBean;
import com.example.kliven.because.intentbean.NetDiscoverBean;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class DiscoverFragment extends Fragment implements View.OnClickListener {

    private RequestQueue queue;
    private List<DiscoverBean> discoverBeans;
    private DiscoverAdapter discoverAdapter;
    private Context context;
    private String url = "http://yinweiapi.paidui.com:81/jms/fetchData.do?procedureName=app_discover_index&parameters=%7B%22_city_id%22:%220%22%7D&memberId=289167";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.discover_fragment, null);

        view.findViewById(R.id.ibtn_fujin).setOnClickListener(this);
        view.findViewById(R.id.ibtn_paihang).setOnClickListener(this);
        view.findViewById(R.id.ibtn_daren).setOnClickListener(this);
        view.findViewById(R.id.ibtn_caixi).setOnClickListener(this);
        view.findViewById(R.id.ibtn_shicai).setOnClickListener(this);
        view.findViewById(R.id.ibtn_kouwei).setOnClickListener(this);

        return view;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ibtn_fujin:
                Intent nearbyIntent = new Intent(getActivity(), NearbyActivity.class);
                startActivity(nearbyIntent);
                getActivity().overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
                break;

            case R.id.ibtn_paihang:
                Intent rankingIntent = new Intent(getActivity(), RankingActivity.class);
                startActivity(rankingIntent);
                getActivity().overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
                break;

            case R.id.ibtn_daren:
                Intent recommendIntent = new Intent(getActivity(), RecommendActivity.class);
                startActivity(recommendIntent);
                getActivity().overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
                break;

            case R.id.ibtn_caixi:
                showCuisine();
                break;

            case R.id.ibtn_shicai:
                showIngredients();
                break;

            case R.id.ibtn_kouwei:
                showTaste();
                break;
        }
    }

    private void showCuisine() {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        View layout = LayoutInflater.from(getActivity()).inflate(R.layout.discover_list, null);
        final ListView myListView = (ListView) layout.findViewById(R.id.list_View);
        builder = new AlertDialog.Builder(getActivity());
        builder.setView(layout);
        queue = Volley.newRequestQueue(getActivity());
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                discoverBeans = new ArrayList<>();
                Gson gson = new Gson();
                NetDiscoverBean netDiscoverBean = gson.fromJson(response, NetDiscoverBean.class);
                for (int i = 0; i < 1; i++) {
                    DiscoverBean discoverBean = new DiscoverBean();

                    String info = netDiscoverBean.getData().getCaixi_item().getProperty_list();

                    String[] strings = info.split("@");
                    String [] myString = new String[strings.length];
                    for(int j = 0; j < strings.length ; j ++){
                        myString[j] = strings[j].substring(strings[j].indexOf(";")+1);
                    }

//                    for (int j = 0; j < netDiscoverBean.getData().getCaixi_item().getProperty_list().length(); j++) {
//                        String mainInfo = info.substring(4, 6);
//                        info.spli
//                        discoverBean.setProperty_list(mainInfo);
//                        Log.e("DiscoverFragment", netDiscoverBean.getData().getCaixi_item().getProperty_list());
//                        discoverBeans.add(discoverBean);
//                    }
                    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_single_choice,myString);

//                    discoverAdapter = new DiscoverAdapter(getActivity(), discoverBeans);
                    myListView.setAdapter(arrayAdapter);

                }


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        queue.add(request);

        builder.setTitle("菜系");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });
        builder.show();
    }

    private void showIngredients() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        View layout = LayoutInflater.from(getActivity()).inflate(R.layout.discover_list, null);
        final ListView myListView = (ListView) layout.findViewById(R.id.list_View);
        builder = new AlertDialog.Builder(getActivity());
        builder.setView(layout);
        queue = Volley.newRequestQueue(getActivity());
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                discoverBeans = new ArrayList<>();
                Gson gson = new Gson();
                NetDiscoverBean netDiscoverBean = gson.fromJson(response, NetDiscoverBean.class);
                for (int i = 0; i < 1; i++) {
//                    DiscoverBean discoverBean = new DiscoverBean();

                    String info = netDiscoverBean.getData().getShicai_item().getProperty_list();

                    String[] strings = info.split("@");
                    String [] myString = new String[strings.length];
                    for(int j = 0; j < strings.length ; j ++){
                        myString[j] = strings[j].substring(strings[j].indexOf(";")+1);
                    }

//                    for (int j = 0; j < netDiscoverBean.getData().getCaixi_item().getProperty_list().length(); j++) {
//                        String mainInfo = info.substring(4, 6);
//                        info.spli
//                        discoverBean.setProperty_list(mainInfo);
//                        Log.e("DiscoverFragment", netDiscoverBean.getData().getCaixi_item().getProperty_list());
//                        discoverBeans.add(discoverBean);
//                    }
                    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_single_choice,myString);

//                    discoverAdapter = new DiscoverAdapter(getActivity(), discoverBeans);
                    myListView.setAdapter(arrayAdapter);

                }


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        queue.add(request);

        builder.setTitle("食材");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });
        builder.show();
    }

    private void showTaste() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        View layout = LayoutInflater.from(getActivity()).inflate(R.layout.discover_list, null);
        final ListView myListView = (ListView) layout.findViewById(R.id.list_View);
        builder = new AlertDialog.Builder(getActivity());
        builder.setView(layout);
        queue = Volley.newRequestQueue(getActivity());
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                discoverBeans = new ArrayList<>();
                Gson gson = new Gson();
                NetDiscoverBean netDiscoverBean = gson.fromJson(response, NetDiscoverBean.class);
                for (int i = 0; i < 1; i++) {
                    DiscoverBean discoverBean = new DiscoverBean();

                    String info = netDiscoverBean.getData().getKouwei_item().getProperty_list();

                    String[] strings = info.split("@");
                    String [] myString = new String[strings.length];
                    for(int j = 0; j < strings.length ; j ++){
                        myString[j] = strings[j].substring(strings[j].indexOf(";")+1);
                    }

//                    for (int j = 0; j < netDiscoverBean.getData().getCaixi_item().getProperty_list().length(); j++) {
//                        String mainInfo = info.substring(4, 6);
//                        info.spli
//                        discoverBean.setProperty_list(mainInfo);
//                        Log.e("DiscoverFragment", netDiscoverBean.getData().getCaixi_item().getProperty_list());
//                        discoverBeans.add(discoverBean);
//                    }
                    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_single_choice,myString);

//                    discoverAdapter = new DiscoverAdapter(getActivity(), discoverBeans);
                    myListView.setAdapter(arrayAdapter);

                }


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        queue.add(request);

        builder.setTitle("口味");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });
        builder.show();
    }
}
